const util = require('util');
const debug = util.debuglog('oil-game:login:api');
const Time = require("../../../util/time");
const {checkUserSign, getUserSignData} = require('../../area/lib/signCache');

// 有人邀请 更新父用户的徒弟 和 祖父用户的徒孙
exports.addApprentice = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.addApprentice(session, uid, session.frontendId, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 更新新注册用户的师爷用户的数据
exports.addDisciple = function (session, parent_id) {
    return new Promise((resolve, reject) => {
        this.app.rpc.login.loginRemote.addDisciple(session, parent_id, session.frontendId, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 增加邀请用户的日常任务奖励
exports.addInviteReward = function (session, parent_id) {
    return new Promise((resolve, reject) => {
        this.app.rpc.mission.missionRemote.increaseInviteReward(session, parent_id, session.frontendId, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 判断用户今日是否观看过视频
exports.userWatchVideoTodayApi = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.video.videoRemote.userVideoExistsRemote(session, uid, session.frontendId, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 获取升级系统中首页需要的数据
exports.getUpgradeData = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.upgrade.upgradeRemote.indexData(session, uid, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 获取待收取宝箱数量
exports.getWaitBoxNum = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.box.boxRemote.getWaitBoxNum(session, uid, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

// 初始化升级信息数据表
exports.initUpgradeByMongo = function (session, uid) {
    debug("用户" + uid + " 初始化升级信息数据表")
    return new Promise((resolve, reject) => {
        this.app.rpc.upgrade.upgradeRemote.initUpgradeByMongo(session, uid, function (err, data) {
            if (err) {
                debug("用户" + uid + " 初始化升级信息数据表发生异常")
                return reject(err);
            }
            debug("用户" + uid + " 初始化升级信息数据表，结果" + JSON.stringify(data))
            resolve(data);
        });
    });
}

// 获取离线金币
exports.getLogOutCoins = function (session, last_login_time, last_logout_time) {
    let {uid} = session;
    debug("用户 %s 获取离线金币奖励", uid)
    return new Promise((resolve, reject) => {
        this.app.rpc.upgrade.upgradeRemote.getLogOutCoins(session, uid, last_login_time, last_logout_time, function (err, data) {
            if (err) {
                debug("用户 %s 获取离线金币奖励异常 %s", uid, JSON.stringify(err));
                return reject(err);
            }
            debug("用户 %s 获取离线金币奖励成功 %s", uid, JSON.stringify(data));
            resolve(data);
        });
    });
}

exports.checkSignToday = async function (uid) {
    let today = Time.getTodayDate();
    if (await checkUserSign.call(this, uid)) {
        let signData = await getUserSignData.call(this, uid);
        if (signData) {
            //检测是否已经签到过
            for (let key in signData) {
                let data = signData[key];
                if (data.date == today) {
                    return data.is_single;
                }
            }
        } else {
            return 0;
        }
    } else {
        return 0
    }
}

// 返回最新的几条交易信息
exports.getNowExchangeList = function (session, num) {
    debug("获取最新的 %s 条石油交易记录", num)
    return new Promise((resolve, reject) => {
        this.app.rpc.exchange.exchangeRemote.getNowExchangeList(session, num, function (err, data) {
            if (err) {
                debug("获取最新的 %s 条交易记录异常 %s", num, JSON.stringify(err));
                return reject(err);
            }
            debug("获取最新的 %s 条交易记录成功 %s", num, JSON.stringify(data));
            resolve(data);
        });
    });
}

// 返回最新的几条石油精华交易信息
exports.getNowExchangeEssenceList = function (session, num) {
    debug("获取最新的 %s 条精华交易记录", num)
    return new Promise((resolve, reject) => {
        this.app.rpc.exchangeEssence.exchangeRemote.getNowExchangeList(session, num, function (err, data) {
            if (err) {
                debug("获取最新的 %s 条交易记录异常 %s", num, JSON.stringify(err));
                return reject(err);
            }
            debug("获取最新的 %s 条交易记录成功 %s", num, JSON.stringify(data));
            resolve(data);
        });
    });
}

//获取在线用户的每秒产金量
exports.getProductRpc = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.upgrade.upgradeRemote.getPreProduct(session, uid, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}

//判断在线用户双倍奖励是否开启
exports.isDoubleOpenRpc = function (session, uid) {
    return new Promise((resolve, reject) => {
        this.app.rpc.upgrade.upgradeRemote.isInDoubleRemote(session, uid, function (err, data) {
            if (err) return reject(err);
            resolve(data);
        });
    });
}
